package main

import (
	"kuafu/cmd"
	"kuafu/progress/log"
	"os"
	"os/exec"
)

func main() {

	args := os.Args
	daemon := false
	for k, v := range args {
		if v == "-d" {
			daemon = true
			args[k] = ""
		}
	}

	if daemon {
		asyncExec(args...)
		return
	}

	//启动
	commandFactory := new(cmd.LogBeatCommandFactory)
	err := commandFactory.Command().Execute()
	if err != nil {
		log.Log.Error("创建命令行失败：%s", err)
	}

}

func asyncExec(args ...string) {
	var arg []string
	if len(args) > 1 {
		arg = args[1:]
	}
	command := exec.Command(args[0], arg...)
	command.Env = os.Environ()
	err := command.Start()
	if err != nil {
		log.Log.Errorf("脚本执行失败: %s", err)
		panic(err)
	}
}
